package com.health.mapp.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.health.db.model.GoodsDataUser;
import com.health.db.model.GoodsDataUserExample;
import com.health.db.service.sm.IGoodsDataService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.QueryHealthRecordReq;
import com.health.mapp.bean.QueryHealthRecordResp;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * @author HiRoad
 *
 */
public class QueryHealthRecordImpl implements ActionInterface {
	
	private IGoodsDataService goodsDataService ;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		QueryHealthRecordReq req=(QueryHealthRecordReq) reqBean;
		QueryHealthRecordResp _resp= new QueryHealthRecordResp();
		
		List<GoodsDataUser> recordList = new ArrayList<GoodsDataUser>();
		GoodsDataUserExample temp = new GoodsDataUserExample();
		
		Integer pageNo=req.getPageNo();
		Integer pageSize=req.getPageNum();
		
		if(null!=pageNo&&null!=pageSize){
			temp.setOffset((pageNo.intValue()-1)*pageSize.intValue());
			temp.setPageSize(pageSize.intValue());
		}
	
		//工单Id
		 String recId= req.getRecId();
		//医生编号
		 String docId =req.getDocId();
		//用户编号
		 String userId =req.getUserId();
		//状态
		 String state =req.getState();
		 
		 //宝宝编号
		 String patientId = req.getPatientId();
		
		 //医生编号 用户编号 订单号 取一
		 if(StringUtils.isNotBlank(recId)){
			 recordList = goodsDataService.selectGoodsDataUserList(null, Long.valueOf(recId));
		}else if(StringUtils.isNotBlank(userId)){
			
			
			if (StringUtils.isNotBlank(patientId)) {
				temp.setPatientId(Long.valueOf(patientId));
			}
			
			temp.setOwnerId(Long.valueOf(userId));
			temp.setOperFlag((byte) Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_5.intValue());
			 recordList = goodsDataService.selectGoodsDataUserList(temp, null);
		}else if(StringUtils.isNotBlank(docId)){
			
			
			//单独查询还是复合查询
			 if(StringUtils.isBlank(state)) {
				 _resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
					_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
					return _resp;
			}
			if (state.contains(",")) {
				
				 List<Byte> runstatusids = new ArrayList<Byte>();
				 String[] orders = state.split(",");
				 for (int i = 0; i < orders.length; i++) {
					 runstatusids.add(Byte.valueOf(orders[i]));
				}
				 temp.setOperFlags(runstatusids);
				 temp.setAccepterId(Long.valueOf(docId));
			}else{
				temp.setOperFlag(Byte.valueOf(state));
				if (Integer.valueOf(state).intValue()==Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_2.intValue()){
					
				}else
				 if (Integer.valueOf(state).intValue()==Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_3.intValue()||
						 Integer.valueOf(state).intValue()==Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_4.intValue()) {
					temp.setAccepterId(Long.valueOf(docId));
				}else{
					 _resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
					 _resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
					return _resp;
				}
			}
			
			recordList = goodsDataService.selectGoodsDataUserList(temp, null);
		}else {
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
		 _resp.setRecordList(recordList);
		_resp.setPageNo(pageNo);
		_resp.setPageNum(pageSize);
		_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
 
		return _resp;
	}

	public IGoodsDataService getGoodsDataService() {
		return goodsDataService;
	}

	public void setGoodsDataService(IGoodsDataService goodsDataService) {
		this.goodsDataService = goodsDataService;
	}
}
